<?php

namespace MoChat\Salesman\Action;

use Hyperf\DbConnection\Db;
use MoChat\Framework\Action\AbstractAction;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use MoChat\App\Common\Middleware\DashboardAuthMiddleware;
use MoChat\Framework\Request\ValidateSceneTrait;
use MoChat\Framework\Exception\CommonException;
use MoChat\Framework\Constants\ErrorCode;

/**
 * 打卡记录
 * @Controller
 */
class ClockInRecord extends AbstractAction
{
    use ValidateSceneTrait;

    const TYPE = [
        1 => '门店',
        2 => '诊所',
        3 => '连锁',
        4 => '商业公司',
    ];

    private function timeCheck($data)
    {
        return isset($data['start_time']) && isset($data['end_time']) && !empty($data['start_time']) && !empty($data['end_time']);
    }

    /**
     * @Middlewares({
     *     @Middleware(DashboardAuthMiddleware::class),
     * })
     * @RequestMapping(path="/dashboard/salesman/clockRecord", methods="GET")
     */
    public function handle()
    {
        $user = user();
        // 判断用户绑定企业信息
        if (!isset($user['corpIds']) || count($user['corpIds']) !== 1) {
            throw new CommonException(ErrorCode::INVALID_PARAMS, '未选择登录企业，不可操作');
        }
        $data = $this->request->all();
        $list = Db::table('clock_record as record')
            ->join('client as client', 'client.id', '=', 'record.client_id')
            ->join('work_employee as employee', 'employee.id', '=', 'record.work_employee_id')
            ->when(isset($data['name'])&& !empty($data['name']), function ($query) use ($data){
                $query->where('client.enterprise_name','like','%'.$data['name'].'%');
            })
            ->when(isset($data['employee_id'])&& $data['employee_id']!=='', function ($query) use ($data){
                $query->where('employee.id','=',$data['employee_id']);
            })
            ->when(isset($data['employee_id'])&& $data['employee_id']!=='', function ($query) use ($data){
                $query->where('employee.id','=',$data['employee_id']);
            })
            ->when(isset($data['client_id'])&& $data['client_id']!=='', function ($query) use ($data){
                $query->where('client.id','=',$data['client_id']);
            })
            ->when(isset($data['client_name'])&& $data['client_name']!=='', function ($query) use ($data){
                $query->where('client.enterprise_name','like','%'.$data['client_name'].'%');
            })
            ->when($this->timeCheck($data), function ($query) use ($data) {
                $query->where('record.created_at', '>=', $data['start_time']);
                $query->where('record.created_at', '<', $data['end_time']);
            })
            ->when(isset($data['time']) && is_array($data['time']) && $data['time'], function ($query) use ($data) {
                $query->where('record.created_at', '>=', $data['time'][0]);
                $query->where('record.created_at', '<', $data['time'][1].' 23:59:59');
            })
            ->orderBy('record.created_at', 'desc')
            ->select([
                'record.id',
                'client.enterprise_name',//客户名称
                'client.b2b_user_type',//客户类型
                'record.created_at',//签到时间
                'employee.name',//签到人
                'record.address',//签到地址
                'record.lng',//
                'record.lat',//
                'record.image',//图片
                'record.remark'
            ])->paginate((int)($data['perPage'] ?? 15));
        if (!empty($list->items())) {
            foreach ($list->items() as &$item) {
                $item->b2b_user_type = self::TYPE[$item->b2b_user_type] ?? '';
            }
            unset($item);
        }
        return $list;
    }

    /**
     * 验证规则.
     */
    protected function rules(): array
    {
        return [

        ];
    }

    /**
     * 获取已定义验证规则的错误消息.
     */
    protected function messages(): array
    {
        return [

        ];
    }
}
